<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-position="right"
      label-suffix=":"
      label-width="120px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('problemItemLang.problemItemStatus')">
            <el-radio-group v-model="queryParams.issueStatus">
              <el-radio-button label="null">{{ $t("all") }}</el-radio-button>
              <el-radio-button
                v-for="item in statusOptions"
                :key="item.key"
                :label="item.value"
              >
                {{ $t("statusLang." + item.title) }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="noType">
                <el-option
                  :value="0"
                  :label="$t('problemItemLang.warehouseOutDocNo')"
                />
                <el-option
                  :value="1"
                  :label="$t('problemItemLang.rigDocNo')"
                />
                <el-option
                  :value="2"
                  :label="$t('problemItemLang.providerNo')"
                />
                <el-option
                  :value="3"
                  :label="$t('backOrderLang.blendNum')"
                />
              </el-select>
            </template>
            <el-input
              v-model="anyNo"
              :placeholder="$t('backOrderLang.backPleaseEnterTip')"
              type="textarea"
              :rows="3"
              clearable
              style="width: 350px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotListAllStatusList"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('problemItemLang.problemType')">
            <el-select
              v-model="queryParams.issueType"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 300px"
            >
              <el-option
                v-for="item in problemTypeOptions"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item>
            <template #label>
              <el-select v-model="timeType">
                <el-option :value="0" :label="$t('cutOffOrderLang.createDate')" />
                <el-option :value="1" :label="$t('problemItemLang.handleDate')" />
              </el-select>
            </template>
            <el-date-picker
              v-model="timeRange"
              type="daterange"
              value-format="timestamp"
              start-placeholder="开始日期"
              range-separator="至"
              end-placeholder="结束日期"
              style="width: 350px"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            @click="handleAdd"
          >
            {{ $t("add") }}</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="100"
      />
      <el-table-column
        :label="$t('harvestLang.orderInfo')"
        prop="orderNo,providerName,issueStatus"
        align="left"
        min-width="120"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("problemItemLang.warehouseOutDocNo") + ":" }}
            <span>{{ scope.row.orderNo }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.providerNo") + ":" }}
            <span>{{ scope.row.providerTrackingNo }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.problemItemStatus") + ":" }}
            <span>{{ $t("statusLang." + getTitleByValue(statusOptions, scope.row.issueStatus)) }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('problemItemLang.problemInfo')"
        prop="issueType,issueDesc"
        align="left"
        min-width="120"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("problemItemLang.problemType") + ":" }}
            <span>{{ $t(getTitleByValue(problemTypeOptions, scope.row.issueType)) }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.problemDesc") + ":" }}
            <span>{{ scope.row.issueDesc }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('depotLang.depot')"
        min-width="100"
        align="center"
        prop="depotName"
      />
      <el-table-column
        :label="$t('goodsSKULang.customer')"
        min-width="100"
        align="center"
        prop="customerName"
      />
      <el-table-column
        :label="$t('backOrderLang.date')"
        prop="gmtCreated,handleTime"
        align="center"
        min-width="120"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("backOrderLang.createDate") + ":" }}
            <span>{{ scope.row.gmtCreated }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.handleDate") + ":" }}
            <span>{{ scope.row.handleTime }}</span>
          </p>
          <p>
            {{ $t("destructionLang.lastEditDate") + ":" }}
            <span>{{ scope.row.depotCodeAndName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('goodsSKULang.operator')"
        min-width="120"
        prop="createdUserName,handleUserName"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("cutOffOrderLang.creator") + ":" }}
            <span>{{ scope.row.createdUserName }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.handler") + ":" }}
            <span>{{ scope.row.handleUserName }}</span>
          </p>
          <p>
            {{ $t("problemItemLang.lastEditor") + ":" }}
            <span>{{ scope.row.depotCodeAndName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operation')"
        min-width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button v-if="scope.row.issueStatus == 0" type="text" @click="handle(scope.row)">
            {{ $t("handle") }}
          </el-button>
          <el-button type="text" @click="handleDetail(scope.row)">
            {{ $t("info") }}
          </el-button>
          <el-button v-if="scope.row.issueStatus == 0" type="text" @click="handleDelete(scope.row)">
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 新增弹层 -->
    <DepotBaseForm
      v-model="infoParams.id"
      :order-no="infoParams.orderNo"
      :visible.sync="depotDialog"
      :title="title"
      :operation="infoParams.operation"
      @close="closeAdd"
    />
    <!-- 处理详情弹层 -->
    <DetailBaseForm
      v-model="infoParams.id"
      :visible.sync="detailDialog"
      :title="title"
      :operation="infoParams.operation"
      @close="closeInfo"
    />
  </div>
</template>

<script>
import DepotBaseForm from './components/info-form.vue'
import DetailBaseForm from './components/detail-form.vue'
import {
  problemItemStatusOptionDict,
  problemTypeOptionDict
} from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import { getIssuePageList, issueDelete } from '@/api/business/operateManage/problemItem'

export default {
  name: 'Depot',
  components: { DepotBaseForm, DetailBaseForm },
  data() {
    return {
      depotListAllStatusList: [],
      statusType: 0,
      noType: 0,
      anyNo: undefined,
      timeType: 0,
      timeRange: [],
      statusOptions: getDictOptionList(problemItemStatusOptionDict),
      problemTypeOptions: getDictOptionList(problemTypeOptionDict), // 问题件类型
      total: 0,
      loading: false,
      queryParams: {
        issueStatus: null,
        depotId: null,
        issueType: null,
        pageNo: 1,
        pageSize: 10
      },
      tableData: [],
      visibles: false,
      routOrderNo: null,
      getTitleByValue: getTitleByValue,
      depotStatusTag: ['info', 'success', 'danger'],
      infoParams: {
        id: null,
        orderNo: null,
        operation: 0
      },
      depotDialog: false,
      detailDialog: false,
      title: ''
    }
  },
  watch: {
    '$route'(to, from) {
      this.handleRouteParams()
    }
  },
  created() {
    this.getDepotListAllStatus()
    this.getPageList()
    this.handleRouteParams()
  },
  methods: {
    handleRouteParams() {
      // Assuming the parameters are passed via query params
      if (this.$route.query.orderNo) {
        this.routOrderNo = this.$route.query.orderNo
        this.handleAdd()
      }
    },
    // 重置
    async handleReset() {
      this.queryParams = {
        issueStatus: null,
        depotId: null,
        issueType: null,
        pageNo: 1,
        pageSize: 10
      }
      this.anyNo = null
      this.timeRange = []
      this.noType = 0
      await this.$refs.queryParams.resetFields()
      this.getPageList()
    },
    // 新增
    async handleAdd() {
      this.infoParams.operation = 1
      if (this.routOrderNo) {
        this.infoParams.orderNo = this.routOrderNo
      }
      this.title = this.$t('add')
      this.depotDialog = true
    },
    // 处理
    async handle(row) {
      this.infoParams.operation = 1
      this.infoParams.id = row.issueId
      this.title = this.$t('handle')
      setTimeout(() => {
        this.detailDialog = true
      }, 400)
    },
    // 查看
    async handleDetail(row) {
      this.infoParams.operation = 0
      this.infoParams.id = row.issueId
      this.title = this.$t('info')
      setTimeout(() => {
        this.detailDialog = true
      }, 400)
    },
    // 删除
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t('problemItemLang.isDeleteProblemItemTip'),
        this.$t('problemItemLang.isDeleteProblemItem'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await issueDelete({ issueId: row.issueId })
      if (res && res.returnCode === 10000) {
        this.$message({
          type: 'success',
          message: this.$t('handleSuccess')
        })
        this.getPageList()
      }
    },
    // 查询
    async handleQuery() {
      this.queryParams.pageNo = 1
      this.getPageList()
    },
    // 所有仓库状态下拉列表
    async getDepotListAllStatus() {
      const { data } = await getDepotListAllStatus()
      this.depotListAllStatusList = data
    },
    // 获取列表
    async getPageList() {
      this.loading = true
      const anyNo = this.anyNo
      // 退回单号、原物流商单号、原物流单号、混合条件查询
      this.queryParams.orderNo = undefined
      this.queryParams.platformOrderNo = undefined
      this.queryParams.providerTrackingNo = undefined
      this.queryParams.blendNum = undefined
      if (this.noType === 0) {
        this.queryParams.orderNo = anyNo
      }
      if (this.noType === 1) {
        this.queryParams.platformOrderNo = anyNo
      }
      if (this.noType === 2) {
        this.queryParams.providerTrackingNo = anyNo
      }
      if (this.noType === 3) {
        this.queryParams.mixOrderNo = anyNo
      }
      // 时间查询

      const timeRange = this.timeRange
      this.queryParams.startTime = undefined
      this.queryParams.endTime = undefined
      this.queryParams.timeType = undefined
      if (timeRange && timeRange.length > 0) {
        const startDate = new Date(timeRange[0])
        const endDate = new Date(timeRange[1])
        this.queryParams.startTime = startDate.toISOString().slice(0, 10)
        this.queryParams.endTime = endDate.toISOString().slice(0, 10)
        if (this.timeType === 0) {
          this.queryParams.timeType = 'CREATE'
        }
        if (this.timeType === 1) {
          this.queryParams.timeType = 'HANDLE'
        }
      }
      const res = await getIssuePageList({ ...this.queryParams })
      if (res.data) {
        this.tableData = res.data.issueRespList
        this.total = res.data.totalRowNum
      } else {
        this.tableData = []
        this.total = 0
      }
      this.loading = false
    },
    // 关闭弹出层
    closeAdd() {
      this.infoParams.id = null
      this.infoParams.operation = 0
      this.depotDialog = false
      this.getPageList()
    },
    // 关闭弹出层
    closeInfo() {
      this.infoParams.id = null
      this.infoParams.operation = 0
      this.detailDialog = false
    },
    // 切换页码
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getPageList()
    },
    indexAdd(index) {
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>

<style scss="lang" scoped></style>
